const express = require('express')
const qs = require('querystring')
//路由容器
const trade = express.Router()
const axios = require('axios')
const jwt = require("jsonwebtoken")//解析token

// 跳转订单管理页面
trade.get('/orderMenu', (req, res) => {
    res.render('admin/orderMenu',{
        userInfo: jwt.decode(req.session.token)
    })
})
// 订单管理————获取所有商品
trade.get('/ManagerAllOrders', (req, res) => {
    let {page ,size } = req.query;
    axios({
        url: '/consumer/ManagerAllOrders',
        method: 'GET',
        params: {
            page: page,
            size: size
        }
    })
    .then(data => {
        res.send(data.data)
        console.log(data.data)
        
    })
    .catch(err => {
        console.log(err);
    })
})
// 订单管理————根据订单号查找
trade.get('/findAOrder', (req, res) => {
    let {orderNo} = req.query;
    axios({
        url: `/consumer/findAOrder/${orderNo}`,
        method: 'GET',
    })
    .then(data => {
        res.send(data.data)
        console.log(data.data)
        
    })
    .catch(err => {
        console.log(err);
    })
})
// 订单管理————根据时间查询
trade.get('/findSuchOrders', (req, res) => {
    let {time,page,size} = req.query;
    axios({
        url: `/consumer/findSuchOrders/${time}`,
        method: 'GET',
        params: {
            page:page,
            size:size
        }
    })
    .then(data => {
        res.send(data.data)
        console.log(data.data)
        
    })
    .catch(err => {
        console.log(err);
    })
})
// 订单管理————根据类别查询
trade.get('/findByBigId', (req, res) => {
    let {bigId} = req.query;
    axios({
        url: `/consumer/findByBigId`,
        method: 'GET',
        params: {
            bigId:bigId
        }
    })
    .then(data => {
        res.send(data.data)
        console.log(data.data)
        
    })
    .catch(err => {
        console.log(err);
    })
})
// 订单管理————分类查询(0:未支付；1：已支付；2：已删除)
trade.get('/findByClass', (req, res) => {
    let {setting,page,size} = req.query;
    axios({
        url: '/consumer/findByClass',
        method: 'GET',
        params: {
            page:page,
            size:size,
            setting:setting
        }
    })
    .then(data => {
        res.send(data.data)
        console.log(data.data)
        
    })
    .catch(err => {
        console.log(err);
    })
})
// 订单管理————删除一个订单
trade.patch('/updateAOrder', (req, res) => {
    let {orderId } = req.query;
    axios({
        url: '/consumer/updateAOrder',
        method: 'PATCH',
        params: {
            orderId:orderId
        }
    })
    .then(data => {
        res.send(data.data)
        console.log(data.data)
        
    })
    .catch(err => {
        console.log(err);
    })
})  
// 订单管理————批量删除订单
trade.patch('/updateAOrders', (req, res) => {
    let {ids } = req.query;
    axios({
        url: '/consumer/updateAOrders',
        method: 'PATCH',
        params: {
            ids:ids
        }
    })
    .then(data => {
        res.send(data.data)
        console.log(data.data)
        
    })
    .catch(err => {
        console.log(err);
    })
})  
// 更改订单信息
trade.patch('/updateOrders', (req, res) => {
    let {ids} = req.query;
    axios({
        url: '/consumer/updateOrders',
        method: 'PATCH',
        params: {
           ids:ids
        }
    })
    .then(data => {
        res.send(data.data)
        console.log(data.data)
        
    })
    .catch(err => {
        console.log(err);
    })
})
// 跳转订单详情页面
trade.get('/orderMenuDetail', (req, res) => {
    let {orderNo} = req.query;
    console.log("-=-=-=-=-=-");
    console.log(orderNo);
    axios({
        url: `/consumer/findReallyOrder/${orderNo}`,
        method: 'GET',
    })
    .then(data => {
        // res.send(data.data)
        console.log(data.data)
        if(data.data.code==20000){
            res.render('admin/orderMenuDetail',{
                userInfo: jwt.decode(req.session.token),
                orderInfo:data.data.data
            })
        }else{
            res.redirect('admin/error')
        }
        
    })
    .catch(err => {
        console.log(err);
    })
})
// 跳转交易金额
trade.get('/orderAmount', (req, res) => {
    axios({
        url: '/consumer/showMoney',
        method: 'GET',
    })
    .then(data => {
        console.log(data.data)
        if(data.data.code == 20000){

            res.render('admin/orderAmount',{
                info:data.data.data,
                userInfo: jwt.decode(req.session.token)    
            })
        }
    })
    .catch(err => {
        console.log(err);
    })
})
trade.get('/findChart', (req, res) => {
    axios({
        url: '/consumer/findChart',
        method: 'GET',
    })
    .then(data => {
        console.log(data.data)
        if(data.data.code == 20000){
            res.send(data.data)
        }
    })
    .catch(err => {
        console.log(err);
    })
})

module.exports=trade;